Ext.ns('org.systemsbiology.codefor');

console.log('org.systemsbiology.codefor! available at:' + org.systemsbiology.codefor);

var Container = Ext.extend(Object, {
    currentState: {},
    
    logo: {
       url: "http://cytoscapeweb.cytoscape.org/img/logos/cw_s.png"
    },
    
    constructor: function(container) {
        console.log("org.systemsbiology.codefor.GDSApiContainer(" + container + ")");
        Ext.apply(this, {contentEl:Ext.get(container)});
    },

    draw: function(data, options) {
        console.log("org.systemsbiology.codefor.GDSApiContainer.draw(" + data + "," + options + ")");
        this.schema = options.schema;
        this.mappings = options.mappings;

        var params = { tqx: "out:json_array" };
        if (data.query) { params.tq = data.query; }

        Ext.Ajax.request({
            url: data.uri + "/query",
            method: "GET",
            params: params,
            success: this.LoadNetwork,
            failure: function(o) {
                Ext.getDom(this.contentEl).innerHTML = o.responseText;
            },
            scope: this
        });
    },
    
    LoadNetwork: function(o) {
        var jsonData = Ext.util.JSON.decode(o.responseText);

        var sourceId = this.mappings.sourceId;
        var sourceLabel = this.mappings.sourceLabel;
        var targetId = this.mappings.targetId;
        var targetLabel = this.mappings.targetLabel;

        var node_map = {};
        var node_array = [];
        var edge_array = jsonData.map(function(row) {
            var sourceNodeId = "" + row[sourceId];
            var targetNodeId = "" + row[targetId];

            var sourceNode = node_map[sourceNodeId];
            if (sourceNode === undefined) {
                sourceNode = { id : "" + sourceNodeId, label : "" + row[sourceLabel] };
                node_array.push(sourceNode);
                node_map[sourceNodeId] = sourceNode;
            }

            var targetNode = node_map[targetNodeId];
            if (targetNode === undefined) {
                targetNode = {id : "" + targetNodeId, label : "" + row[targetLabel] };
                node_array.push(targetNode);
                node_map[targetNodeId] = targetNode;
            }

            return { id: sourceNodeId + '-' + targetNodeId,
                     label : row[sourceLabel] + ' linked to ' + row[targetLabel],
                     source: sourceNodeId, target: targetNodeId
            };
        });

        var vis = new org.cytoscapeweb.Visualization(this.contentEl.id, {
            swfPath: "https://informatics-apps.systemsbiology.net/cytoscapeweb_v0.8/swf/CytoscapeWeb",
            flashInstallerPath: "https://informatics-apps.systemsbiology.net/cytoscapeweb_v0.8/swf/playerProductInstall"
        });
        vis.draw({
            network: { data: {nodes: node_array, edges: edge_array}, dataSchema: this.schema },
            nodeTooltipsEnabled: true,
            edgeTooltipsEnabled: true
        });
    },

    GetState: function() {
        console.log("org.systemsbiology.codefor.GDSApiContainer.GetState(): TODO: gather state from contained visualization");
        return this.currentState;
    },

    SetState: function(state) {
        this.currentState = state;
        console.log("org.systemsbiology.codefor.GDSApiContainer.SetState(" + state + "): TODO: pass state on to contained visualization");
    }
});

org.systemsbiology.codefor.GDSApiContainer = Container;

org.systemsbiology.pages.apis.events.MessageBus.fireEvent("publish", { 
  key: "org.systemsbiology.codefor.GDSApiContainer",
  payload: org.systemsbiology.codefor.GDSApiContainer
});